<template>
  <div id="app">
    <keep-alive>
    <router-view></router-view>
    </keep-alive>
  </div>
</template>

<script>
import Cookie from 'js-cookie'
import store from './store/index'
import Const from './const'
import Vue from 'vue';
import axios from "@/api/ajax";
import Message from "element-ui/lib/message";

export default {
  name: 'app',
  store:store,
  methods:{
    getPlayType(){
      console.log("shit",this.Global)

      axios.get('/plan_types').then(response => {
        if (response.data.Status === 0) {
          for(let i = 0;i < response.data.Data.length; i ++){
            this.Global.planTypes.push({
              value: response.data.Data[i].id,
              label: response.data.Data[i].type_name
            })
          }
        }
      }).catch(reason => {
        Message.error("网络错误:" + reason);
      });
    },
    open: function () {
          console.log("socket连接成功")
          this.send("hello world")

      },
    error: function () {
        console.log("连接错误")
    },
    getMessage: function (msg) {
        console.log("recv data ",msg.data)
    },
    send: function (params) {
        this.socket.send(params)
    },
    close: function () {
        console.log("socket已经关闭")
    }
  },
  mounted() {
    this.getPlayType()
  },
  created() {
    if(typeof(WebSocket) === "undefined"){
        console.log("您的浏览器不支持socket")
    }else{
        console.log("start connect")
        // 实例化socket
        this.socket = new WebSocket("ws://127.0.0.1:9850/ws")
        // 监听socket连接
        this.socket.onopen = this.open
        // 监听socket错误信息
        this.socket.onerror = this.error
        // 监听socket消息
        this.socket.onmessage = this.getMessage
        this.Global.SetWebsocket(this.socket)
    }

    let prefix = process.env.NODE_ENV === 'production'
        ? "/dist/admin/index.html#"
        : 'http://127.0.0.1:8080';
    let routes = '';
    //console.log(window.location.href.search(prefix));
    if(window.location.href.search(prefix) != -1) {
        routes  = window.location.href.substr(window.location.href.search(prefix) + prefix.length,window.location.href.length);
    }else{
        routes = '/BarIview';
    }
    if((window.location.href.search("article") != -1 ) && (window.location.href.search("articleManage") === -1 )){
        this.$router.push(routes);
        return;
    }
    if (Cookie.get(Const.KEY_USER_INFO)) {
        //如果有获取到cookie证明已经是登录状态，vuex和redirect
        store.commit("setAccount",Cookie.get(Const.KEY_USER_INFO));
        //跳转到主界面
        console.log("current request path not replace: ",window.location.href);
        let route_true = window.location['pathname'].replace('/dist/admin/index.html','/BarIview');
        if(route_true === "/"){
          this.$router.push("/BarIView");
        }else{
          this.$router.push(route_true);
        }
        return;
    }else{
        this.$router.push('/login');
    }
  },
  activated() {
  },
  destroyed() {
    this.socket.onclose = this.close

  },
}
</script>
<style>
body{
  margin: 0px;
  height:100%;
}
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  height: 100%;
}
</style>
